﻿var colorArray = new Array();
colorArray[0] = "Blue";
colorArray[1] = "Green";
colorArray[2] = "Red";
colorArray[3] = "Yellow";
colorArray[4] = "Black";
colorArray[5] = "White";

var wordArray = new Array();
wordArray[0] = "Blå";
wordArray[1] = "Grön";
wordArray[2] = "Röd";
wordArray[3] = "Gul";
wordArray[4] = "Svart";
wordArray[5] = "Vit";


var timeSec = 0, //Seconds
    qTime = 0, //Seconds per question
    qTimerRunning = false,
    qTimerInterval = null,

    inter = null,

    timerRunning = false,
    timeResult,

    score = 200,
    scoreInter = null,
    scoreTimeRunning = false,

    error = 0,
    qIndex = 1, //Counter that keeps track of the number of questions

    correctAnswer,
    isTestRound = false,

    lastColor;

//Function that switches the word and color
function nextQ(btn) {

    if (btn == "btnTest") {
        isTestRound = true;
    }

    $("#lblQNumber").text("Fråga nummer: " + qIndex);
    document.getElementById("buttons").style.display = "";
    document.getElementById("divInformation").style.display = "none";
    $("#btnStart").hide();
    document.getElementById("divColor").style.display = "";
    

    var randColor = colorArray[Math.floor(Math.random() * 6)];

    //If the newly randomized color is tha same as the last one it gets re-randomized
    while (randColor == lastColor) {
        randColor = colorArray[Math.floor(Math.random() * 6)];
    }

    lastColor = randColor; //Stores the current color for the next time this function is called

    var randText = wordArray[Math.floor(Math.random() * 6)];
    var btnNumb = Math.floor((Math.random() * 2) + 1); //Randoms a button number
    
    $("#cWord").text(randText).css({ color: randColor });
    
    if (!isTestRound && qIndex > 10 && qIndex < 20) {
        changeButtonColor();
        $("#lblLevel").text("Nivå 2");
    }

    else if (isTestRound && qIndex >= 4 && qIndex < 6) {
        changeButtonColor();
        $("#lblLevel").text("Nivå 2");
    }

    //Translates color on the buttons
    if (randColor == "Blue") {
        randColor = "Blå";
    }
    if (randColor == "Green") {
        randColor = "Grön";
    }
    if (randColor == "Red") {
        randColor = "Röd";
    }
    if (randColor == "Yellow") {
        randColor = "Gul";
    }
    if (randColor == "Black") {
        randColor = "Svart";
    }
    if (randColor == "White") {
        randColor = "Vit";
    }

    while (randColor == randText) {
        randText = wordArray[Math.floor(Math.random() * 6)];
        $("#cWord").text(randText);
    }


    correctAnswer = randColor;

    $("#btnA" + btnNumb).val(randColor) //Applies a the randomized color as text to the answer button
    
    if ($("#btnA1").val() == randColor) { //If btnA1 got the randomized color, the other button gets the text
        $("#btnA2").val(randText);
    }
    else if ($("#btnA2").val() == randColor) {
        $("#btnA1").val(randText);
    }

    if (!isTestRound) {
        startTimer();
        startQtimer();
    }
    qIndex++;
}

//Randomizes the color of the answer buttons for stage 2 of the test
function changeButtonColor() {
    var rndColor1 = colorArray[Math.floor(Math.random() * 6)];
    var rndColor2 = colorArray[Math.floor(Math.random() * 6)];

    while (rndColor1 == rndColor2) {
        rndColor2 = colorArray[Math.floor(Math.random() * 6)];
    }

    $("#btnA1").css({ backgroundColor: rndColor1 });
    $("#btnA2").css({ backgroundColor: rndColor2 });
}



function next(btn) {
    qTime = 0;
    if (!isTestRound && btn != correctAnswer) {
        error++;
        if (score > 10) {
            score -= 10;
        }
        
    }

    if (isTestRound) {
        document.getElementById("correct").style.display = "block";
        if (btn != correctAnswer) {
            document.getElementById("correct").style.color = "red";
            document.getElementById("correct").innerHTML = "Fel!";
        } else {
            document.getElementById("correct").style.color = "green";
            document.getElementById("correct").innerHTML = "Rätt!";
        }
        $("#correct").animate({
            left: "50px",
            bottom: "90px",
            opacity: 0
        }, 750, function () {
            $("#correct").animate({
                left: "20px",
                bottom: "40px",
                opacity: 1
            });
            document.getElementById("correct").style.display = "none";
        });
    }

    nextQ();

    if(!isTestRound && qIndex > 21) {
        document.getElementById("divColor").style.display = "none";
        document.getElementById("buttons").style.display = "none";
        document.getElementById("divEnd").style.display = "";
        stopTimer();
        $("#lblErrors").text("Antal fel: " + error);
        $("#lblTimeResult").text("Sluttid: " + timeSec);
        $("#lblScore").text("Poäng: " + score);
        //var baseline = PageMethods.GetBl(function (response) { baseline = response; });
        
        PageMethods.GetBl(function (response) {
            $("#lblBase").text("Mätvärde: " + response);
        });
        //$("#lblBase").text("Mätvärde: " + baseline);
        qIndex = 1;
        getStroopResults();
    }
    else if (isTestRound && qIndex > 7) {
        document.getElementById("divColor").style.display = "none";
        document.getElementById("buttons").style.display = "none";
        document.getElementById("divInformation").style.display = "";
        $("#btnStart").show();
        qIndex = 1;
        $("#btnA1").css({ backgroundColor: "#ededed" });
        $("#btnA2").css({ backgroundColor: "#ededed" });
        isTestRound = false;
    }
    
}

//Updates the timer
function timer() {
    timeSec++;
}

function qTimer() {
    if (qTime == 100 && qIndex <= 10) {
        error++;
        levelOne();
        score -= 10
        qTime = 0;
    }
    else if (qTime == 100 && qIndex > 10) {
        error++;
        nextQ();
        score -= 10
        qTime = 0;
    }
    qTime++;
}

function setScore() {
    if (score > 0) {
        score--;
    }
}

//Starts calling the timer()-function every second
function startTimer() {
    if (!timerRunning) {
        inter = setInterval(timer, 1000);
        timerRunning = true;
    }
    if (!scoreTimeRunning) {
        scoreInter = setInterval(setScore, 1000);
        scoreTimeRunning = true;
    }
}

function startQtimer() {
    if (!qTimerRunning) {
        qTimerInterval = setInterval(qTimer, 100);
        qTimerRunning = true;
    }
}

//Stops the interval and saves the time passed
function stopTimer() {
    clearInterval(inter);
    clearInterval(scoreInter);
    clearInterval(qTimerInterval);

    
    timerRunning = false;
    scoreTimeRunning = false;
}

function getStroopResults() {
    var results = [timeSec, error, score];
    PageMethods.getStroopResults(results);
}

function done() {

    window.location = "main.aspx";

}



